package ru.mcfr.oxygen.framework.operations.table;

import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.node.AttrValue;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;
import ru.mcfr.oxygen.framework.operations.McfrBaseAuthorOperation;

import javax.swing.text.BadLocationException;

/**
 * Created by IntelliJ IDEA.
 * User: wstarcev
 * Date: 12.07.11
 * Time: 16:45
 * To change this template use File | Settings | File Templates.
 */
public class InsertColumnOperation extends McfrBaseAuthorOperation{

    {
        description = "Вставка|Удаление столбца в таблицу";

        argumentsNameArray = new String[]{"направление", "вставить/удалить"};

        argumentsTypesArray = new Object[]{ArgumentDescriptor.TYPE_STRING, ArgumentDescriptor.TYPE_STRING};

        argumentsDescriptionsArray = new String[]{"Направление вставки от выбранного столбца: справа/слева", ""};
    }

    @Override
    public void doMainOperation() {
        AuthorNode selectedCell = selectedElement;
        while (!selectedCell.getName().equalsIgnoreCase("ячейка"))
            selectedCell = selectedCell.getParent();

        AuthorNode table = selectedCell;
        while (!table.getName().equalsIgnoreCase("таблица"))
            table = table.getParent();

        int[] thisCoord = authorAccess.getTableAccess().getTableCellIndex((AuthorElement) selectedCell);
        int columnIndex = thisCoord[1];
        int rowsCount = authorAccess.getTableAccess().getTableRowCount((AuthorElement) table);

        for (int i = 0; i < rowsCount; i++){
            AuthorElement thisCell = authorAccess.getTableAccess().getTableCellAt(i, columnIndex, (AuthorElement) table);

            if (argumentsValues.get("вставить/удалить").equalsIgnoreCase("удалить")){

                try {
                    int rowspan = Integer.valueOf(thisCell.getAttribute("rowspan").getValue());
                    i = i + rowspan - 1;
                } catch (Exception e) {
                }

                try {
                    int colspan = Integer.valueOf(thisCell.getAttribute("colspan").getValue());
                    controller.setAttribute("colspan", new AttrValue("" + (--colspan)), thisCell);
                } catch (Exception e) {
                    controller.deleteNode(thisCell);
                }

            } else {

                String newCell = "<ячейка";

                for (int a = 0; a < thisCell.getAttributesCount(); a++) {
                    String attrName = thisCell.getAttributeAtIndex(a);
                    if (attrName.contains("border") | attrName.contains("rowspan")) {
                        String attrValue = thisCell.getAttribute(attrName).getRawValue();
                        newCell += " " + attrName + "=\"" + attrValue + "\"";
                    }
                }

                newCell += "/>";

                int position = thisCell.getEndOffset() + 1;

                try {
                    AuthorDocumentFragment frag = controller.createNewDocumentFragmentInContext(newCell, position);
                    controller.beginCompoundEdit();
                    controller.insertFragment(position, frag);
                    controller.endCompoundEdit();
                } catch (AuthorOperationException e) {
                    e.printStackTrace();
                }

                try {
                    int rowspan = Integer.valueOf(thisCell.getAttribute("rowspan").getValue());
                    i = i + rowspan - 1;
                } catch (Exception e) {
                }

            }

        }

    }

    @Override
    public void doOperationWithSelected() throws AuthorOperationException, BadLocationException {

    }

    @Override
    public void doOperationAtCaretPos() throws AuthorOperationException {

    }
}
